package com.gosophia.conferenceManagement.test.unit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.hibernate.EntityMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.persister.entity.EntityPersister;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.gosophia.commons.DateTranslate;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.starting.entity.ConferenceApplicationJobData;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingJobData;
import com.gosophia.conferenceManagement.starting.entity.ConferenceStartingTransientStore;
import com.gosophia.dataDictionary.entity.DataEntry;
import com.gosophia.dataDictionary.entity.DataValue;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.entity.Mission;
import com.gosophia.userManagement.entity.UserInfo;

/**
 * 实体映射测试类
 * 
 * @author tianchunyu
 * 
 */
@ContextConfiguration(locations = { "classpath:/applicationContext-test.xml" })
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class UTStartingApplicationMapping extends
		AbstractTransactionalJUnit4SpringContextTests {

	@Autowired
	private SessionFactory sessionFactory;

	@Test
	@SuppressWarnings("unchecked")
	public void allClassMapping() throws Exception {
		Session session = sessionFactory.getCurrentSession();
		Map metadata = sessionFactory.getAllClassMetadata();
		for (Object o : metadata.values()) {
			EntityPersister persister = (EntityPersister) o;
			Object e = generateEntity(persister.getMappedClass(EntityMode.POJO));
			if (e == null) {
				continue;
			}
			session.save(e);
			session.flush();
			Serializable id = persister.getIdentifier(e, EntityMode.POJO);
			session.clear();
			session.get(persister.getMappedClass(EntityMode.POJO), id);
		}
	}

	@SuppressWarnings( { "unchecked" })
	private Object generateEntity(Class entityClass) {
		if (entityClass.equals(ConferenceStartingTransientStore.class)) {
			Session session = sessionFactory.getCurrentSession();
			DataEntry e2 = new DataEntry();
			e2.setEntryName(UUID.randomUUID().toString().substring(0, 5));
			e2.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
			e2
					.setEntryDescliption(UUID.randomUUID().toString()
							.substring(0, 5));
			e2.setEntryType("内置");
			session.save(e2);
			session.flush();

			DataValue o = new DataValue();
			o.setOrderIndex(0);
			o.setIsEnabled(true);
			o.setValue(UUID.randomUUID().toString().substring(0, 5));
			o.setKey("001");

			List<DataValue> dataValues = new ArrayList<DataValue>();
			dataValues.add(o);
			e2.setDataValues(dataValues);
			session.save(o);
			session.flush();

			UserInfo e = new UserInfo();
			e.setUserName("马小黑");
			e.setEmail("222@qq.com");
			session.save(e);
			session.flush();

			Mission mission = new Mission();
			mission.setMissionCode("wdzb20110225004");
			mission.setMissionName("伟测试文档001");
			mission.setStartDate(new Date());
			mission.setScheduleEndDate(new Date());
			mission.setDescription("文档任务");
			mission.setMissionState("办理中");
			mission.setProcessInstenceId(15614L);
			mission.setUndertaker(e);
			mission.setCreator(e);
			mission.setUndertaker(e);
			mission.setMissionType(o);
			mission.setImportance(o);
			mission.setMissionDetailURL("getDocMissionDetailAction.action");
			session.save(mission);
			session.flush();

			Job job = new Job();
			job.setHandlerURL("http//");
			job.setDetailURL("http//1");
			job.setStartDate(new Date());
			job.setScheduleEndDate(new Date());
			job.setJobType("sumultee");
			job.setJobState("PROCESSED");
			job.setTokenName("/");
			job.setMission(mission);
			job.setUndertaker(e);
			job.setSource(e);
			session.save(job);
			session.flush();

			ConferenceStartingTransientStore conferenceStartingTransientStore = new ConferenceStartingTransientStore();
			conferenceStartingTransientStore
					.setApplicationInfo("applicationInfo");
			conferenceStartingTransientStore.setConferenceCreator(e);
			conferenceStartingTransientStore
					.setConferenceDescription("conferenceDescription");
			conferenceStartingTransientStore
					.setConferenceModel("noApplication");
			conferenceStartingTransientStore
					.setConferenceName("conferenceName");
			conferenceStartingTransientStore.setConferenceType("largeModel");
			conferenceStartingTransientStore.setConfernceUnderTaker(e);
			conferenceStartingTransientStore.setImportantLevel(o);
			conferenceStartingTransientStore.setJobId(job.getJobId());
			conferenceStartingTransientStore.setMajorParticipators("majorParticipators");
			conferenceStartingTransientStore.setPlanEndTime(DateTranslate.simpleStringToDate("2011-11-11"));
			conferenceStartingTransientStore.setPlanStartTime("planStartTime");
			conferenceStartingTransientStore.setApplicationExplanation("applicationExplanation");
			return conferenceStartingTransientStore;
		} else if (entityClass.equals(ConferenceStartingJobData.class)) {
			Session session = sessionFactory.getCurrentSession();
			DataEntry e2 = new DataEntry();
			e2.setEntryName(UUID.randomUUID().toString().substring(0, 5));
			e2.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
			e2
					.setEntryDescliption(UUID.randomUUID().toString()
							.substring(0, 5));
			e2.setEntryType("内置");
			session.save(e2);
			session.flush();

			DataValue o = new DataValue();
			o.setOrderIndex(0);
			o.setIsEnabled(true);
			o.setValue(UUID.randomUUID().toString().substring(0, 5));
			o.setKey("001");

			List<DataValue> dataValues = new ArrayList<DataValue>();
			dataValues.add(o);
			e2.setDataValues(dataValues);
			session.save(o);
			session.flush();

			UserInfo e = new UserInfo();
			e.setUserName("马小黑");
			e.setEmail("222@qq.com");
			session.save(e);
			session.flush();
			Document e1 = new Document();
			e1.setDocumentCategory("online");
			e1.setDocumentName("我爱中国");
			e1.setDocumentNote("文档备注");
			e1.setDocumentPath("c://");
			e1.setDocumentVersion("1.1");
			session.save(e1);
			session.flush();
			Document e4 = new Document();
			e4.setDocumentCategory("online");
			e4.setDocumentName("我爱中国123123");
			e4.setDocumentNote("文档备注12311");
			e4.setDocumentPath("c://");
			e4.setDocumentVersion("1.1");
			session.save(e4);
			session.flush();
			Mission mission = new Mission();
			mission.setMissionCode("wdzb20110225004");
			mission.setMissionName("伟测试文档001");
			mission.setStartDate(new Date());
			mission.setScheduleEndDate(new Date());
			mission.setDescription("文档任务");
			mission.setMissionState("办理中");
			mission.setProcessInstenceId(15614L);
			mission.setUndertaker(e);
			mission.setCreator(e);
			mission.setUndertaker(e);
			mission.setMissionType(o);
			mission.setImportance(o);
			mission.setMissionDetailURL("getDocMissionDetailAction.action");
			session.save(mission);
			session.flush();

			Job job = new Job();
			job.setHandlerURL("http//");
			job.setDetailURL("http//1");
			job.setStartDate(new Date());
			job.setScheduleEndDate(new Date());
			job.setJobType("sumultee");
			job.setJobState("PROCESSED");
			job.setTokenName("/");
			job.setMission(mission);
			job.setUndertaker(e);
			job.setSource(e);
			session.save(job);
			session.flush();

			ConferenceStartingJobData conferenceStartingJobData = new ConferenceStartingJobData();
			conferenceStartingJobData.setJob(job);
			conferenceStartingJobData.setNextApproval(e);
			conferenceStartingJobData.setOnlineDocument(e1);
			conferenceStartingJobData.setOpinion("处理意见");
			conferenceStartingJobData.setTurningAction("PROCESSED");
			List<Document> attachments = new ArrayList<Document>();
			attachments.add(e1);
			attachments.add(e4);
			conferenceStartingJobData.setAttachments(attachments);
			session.save(e1);
			session.save(e4);
			conferenceStartingJobData.setConferenceName("conferenceName");
			conferenceStartingJobData.setImportantLevel(o);
			conferenceStartingJobData.setPlanEndTime(DateTranslate
					.simpleStringToDate("2011-12-30"));
			conferenceStartingJobData.setConferenceType("largeModel");
			conferenceStartingJobData.setConferenceModel("noApplication");
			conferenceStartingJobData.setConferenceCreator(e);
			conferenceStartingJobData
					.setConferenceDescription("conferenceDescription");
			return conferenceStartingJobData;
		} else if (entityClass.equals(ConferenceApplicationJobData.class)) {
			Session session = sessionFactory.getCurrentSession();
            DataEntry e21 = new DataEntry();
            e21.setEntryName(UUID.randomUUID().toString().substring(0, 5));
            e21.setEntryNameCN(UUID.randomUUID().toString().substring(0, 5));
            e21.setEntryDescliption(UUID.randomUUID().toString().substring(0, 5));
            e21.setEntryType("内置");
            session.save(e21);
            session.flush();

            DataValue o1 = new DataValue();
            o1.setOrderIndex(0);
            o1.setIsEnabled(true);
            o1.setValue(UUID.randomUUID().toString().substring(0, 5));
            o1.setKey("001");

            List<DataValue> dataValues = new ArrayList<DataValue>();
            dataValues.add(o1);
            e21.setDataValues(dataValues);
            session.save(o1);
            session.flush();
            
            UserInfo eu = new UserInfo();
            eu.setUserName("马小黑2"); 
            eu.setEmail("222@qq.com");
            session.save(eu);
            session.flush();
            
            UserInfo u = new UserInfo();
            u.setUserName("马小黑9"); 
            u.setEmail("222@qq.com");
            session.save(u);
            session.flush();
            
            Document e11 = new Document();
            e11.setDocumentCategory("online");
            e11.setDocumentName("我爱中国");
            e11.setDocumentNote("文档备注");
            e11.setDocumentPath("c://");
            e11.setDocumentVersion("1.1");
            session.save(e11);
            session.flush();
            
            Document e41 = new Document();
            e41.setDocumentCategory("online");
            e41.setDocumentName("我爱中国123123");
            e41.setDocumentNote("文档备注12311");
            e41.setDocumentPath("c://");
            e41.setDocumentVersion("1.1");
            session.save(e41);
            session.flush();
            
            Mission mission1 = new Mission();
            mission1.setMissionCode("wdzb20110225004");
            mission1.setMissionName("伟测试文档001");
            mission1.setStartDate(new Date());
            mission1.setScheduleEndDate(new Date());
            mission1.setDescription("文档任务");
            mission1.setMissionState("办理中");
            mission1.setProcessInstenceId(15614L);
            mission1.setUndertaker(eu);
            mission1.setCreator(eu);
            mission1.setUndertaker(eu);
            mission1.setMissionType(o1);
            mission1.setImportance(o1);
            mission1.setMissionDetailURL("getDocMissionDetailAction.action");
            session.save(mission1);
            session.flush();
       
            Job job = new Job();
            job.setHandlerURL("http//");
            job.setDetailURL("http//1");
            job.setStartDate(new Date());
            job.setScheduleEndDate(new Date());
            job.setJobType("sumultee");
            job.setJobState("PROCESSED");
            job.setTokenName("/");
            job.setMission(mission1);
            job.setUndertaker(eu);
            job.setSource(eu);
            session.save(job);
            session.flush();
            
            ConferenceApplicationJobData conferenceApplicationJobData = new ConferenceApplicationJobData();
            conferenceApplicationJobData.setJob(job);
            conferenceApplicationJobData.setNextApproval(eu);
            conferenceApplicationJobData.setOnlineDocument(e11);
            conferenceApplicationJobData.setOpinion("处理意见");
            conferenceApplicationJobData.setTurningAction("PROCESSED");
            List<Document> attachments = new ArrayList<Document>();
            attachments.add(e11);
            attachments.add(e41);
            conferenceApplicationJobData.setAttachments(attachments);
            session.save(e11);
            session.save(e41);
            conferenceApplicationJobData.setPlanStartTime("大约在冬季");
            conferenceApplicationJobData.setMajorIssue("majorIssue");
            conferenceApplicationJobData.setMajorParticipators("马小黑等领导莅临");
            conferenceApplicationJobData.setApplicationInfo("applicationInfo");
            conferenceApplicationJobData.setConfernceUnderTaker(u);
            System.out.println(conferenceApplicationJobData);
            return conferenceApplicationJobData;
			
		} else {
			return null;
		}
	}

}
// /:~
